package com.ghq.controller;

import com.ghq.pojo.Drugs;
import com.ghq.pojo.Result;
import com.ghq.service.DrugsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
public class DrugsController {
    private final DrugsService drugsService;

    @Autowired
    public DrugsController(DrugsService drugsService) {
        this.drugsService = drugsService;
    }

    //查询所有药品
    @GetMapping("/getAllDrugs")
    public Result getAllDrugs(){
        log.info("查询所有药品");
        List<Drugs> drugsList = drugsService.getAllDrugs();
        return Result.success(drugsList);
    }

    //删除药品
    @DeleteMapping("/deleteDrug/{id}")
    public Result deleteDrug(@PathVariable Integer id){
        log.info("删除药品：{}",id);
        drugsService.deleteDrug(id);
        return Result.success();
    }

    //根据ID查询药品
    @GetMapping("/getDrugById/{id}")
    public Result getDrugById(@PathVariable Integer id){
        log.info("根据ID查询药品：{}",id);
        Drugs drugs = drugsService.getDrugById(id);
        return Result.success(drugs);
    }

    //增加药品
    @PostMapping("/addDrug")
    public Result addDrug(@RequestBody Drugs drugs){
        log.info("增加药品：{}",drugs);
        drugsService.addDrug(drugs);
        return Result.success();
    }

    //修改药品
    @PutMapping("/updateDrug")
    public Result updateDrug(@RequestBody Drugs drugs){
        log.info("修改药品：{}",drugs);
        drugsService.updateDrug(drugs);
        return Result.success();
    }

    //模糊查询药品
    @GetMapping("/getDrugByNameAndCategory")
    public Result getDrugByNameAndCategory(@RequestParam("name") String name,@RequestParam("category") String category){
        log.info("模糊查询药品{}，{}",name,category);
        List<Drugs> drugsList = drugsService.getDrugByNameAndCategory(name,category);
        return Result.success(drugsList);
    }

    //批量删除药品
    @DeleteMapping("/deleteDrugs/{ids}")
    public Result deleteDrugs(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}", ids);
        drugsService.deleteDrugs(ids);
        return Result.success();
    }
}
